package com.swu.education.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
/**
 * Created by nana on 2018/5/6.
 */

public class ConnectUrl {
  public static double sendGet(String senOne,String senTwo) throws IOException {
    String charset = "UTF-8";
    senOne = URLEncoder.encode(senOne, charset);
    senTwo = URLEncoder.encode(senTwo, charset);
    String path = "http://222.198.118.211/api?sent_one="+senOne+"&sent_two="+senTwo;
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5 * 1000);// 设置连接超时时间为5秒
    conn.setReadTimeout(20 * 1000);// 设置读取超时时间为20秒
    // 使用 URL 连接进行输出，则将 DoOutput标志设置为 true
    conn.setDoOutput(true);

    conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
    String msg = "";// 保存调用http服务后的响应信息
    // 如果请求响应码是200，则表示成功
    if (conn.getResponseCode() == 200) {
      // HTTP服务端返回的编码是UTF-8,故必须设置为UTF-8,保持编码统一,否则会出现中文乱码
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
      msg = in.readLine();
      in.close();
    }
    conn.disconnect();// 断开连接
    //msg = msg.substring(0,4);
    double resultMsg = Double.parseDouble(msg);
    return resultMsg;
  }
}


